package com.zjhd.sealcutting.service.impl;

import com.google.common.collect.Maps;
import com.zjhd.sealcutting.dao.PressOrganizationMapper;
import com.zjhd.sealcutting.domain.PressOrganization;
import com.zjhd.sealcutting.param.PressOrganizationSearchQuery;
import com.zjhd.sealcutting.service.PressOrganizationService;
import com.zjhd.sealcutting.utils.DateUtil;
import com.zjhd.sealcutting.utils.response.ResponseResult;
import com.zjhd.sealcutting.utils.response.ResultGenerator;
import com.zjhd.sealcutting.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO  印机构管理
 *
 * @author wangchao
 * @version 1.0
 * @date 2020/11/27 15:05
 */
@Service
public class PressOrganizationServiceImpl implements PressOrganizationService {

    @Autowired
    private PressOrganizationMapper pressOrganizationMapper;

    /**
     * TODO  分页查询印机构
     * @author wangchao
     * @date 2020/11/27 15:23
     * @param
     * @return
     */
    @Override
    public SearchResult queryPressOrganizationParamlist(PressOrganizationSearchQuery param) throws Exception {
        //定义map 封装查询参数到map中
        Map<String, Object> map = Maps.newLinkedHashMap();
        if(param.getType()!=null){
            map.put("type",param.getType());
        }
        if(param.getState()!=null){
            map.put("state",param.getState());
        }
        //当前页  分页数
        map.put("pageIndex", (Integer.parseInt(param.getPageIndex()) - 1) * Integer.parseInt(param.getPageSize()));
        map.put("pageSize", Integer.parseInt(param.getPageSize()));
        //查询数据
        List<PressOrganization> pressOrganizations=pressOrganizationMapper.queryParamPressOrganizationList(map);
        long totalCount=pressOrganizationMapper.queryParamPressOrganizationCount(map);
        if(pressOrganizations.size()>0){
            return new SearchResult.Builder<List<PressOrganization>>()
                    .setCurrent(Integer.parseInt(param.getPageIndex()))
                    .setPageSize(Integer.parseInt(param.getPageSize()))
                    .setTotalCount(totalCount)
                    .setPagedItems(pressOrganizations == null ? new ArrayList() : pressOrganizations)
                    .build();
        }else {
            return new SearchResult.Builder().setPagedItems(new ArrayList()).build();
        }
    }

    /**
     * TODO  新增印机构
     * @author wangchao
     * @date 2020/11/27 15:23
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult insertPressOrganization(PressOrganization pressOrganization) throws Exception {
        try {
            pressOrganization.setCreateTime(DateUtil.getCurrentTime());
            int i=pressOrganizationMapper.insert(pressOrganization);
            if(i>0){
                return ResultGenerator.genSuccessResult("新增印机构成功!");
            }else {
                return ResultGenerator.genFailResult("新增印机构失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("新增印机构失败!");
    }

    /**
     * TODO 修改印机构
     * @author wangchao
     * @date 2020/11/27 15:24
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updatePressOrganization(PressOrganization pressOrganization) throws Exception {
        try {
            int i=pressOrganizationMapper.updateByPrimaryKeySelective(pressOrganization);
            if(i>0){
                return ResultGenerator.genSuccessResult("修改印机构成功!");
            }else {
                return ResultGenerator.genFailResult("修改印机构失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("修改印机构失败!");
    }

    /**
     * TODO  删除印机构
     * @author wangchao
     * @date 2020/11/27 15:24
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult delPressOrganization(Integer id) throws Exception {
        try {
            int i=pressOrganizationMapper.deleteByPrimaryKey(id);
            if(i>0){
                return ResultGenerator.genSuccessResult("删除印机构成功!");
            }else {
                return ResultGenerator.genFailResult("删除印机构失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("删除印机构失败!");
    }

    /**
     * TODO  根据ID查询印机构详情
     * @author wangchao
     * @date 2020/11/27 15:24
     * @param
     * @return
     */
    @Override
    public PressOrganization queryByIdPressOrganization(Integer id) throws Exception {
        return pressOrganizationMapper.selectByPrimaryKey(id);
    }

    /**
     * TODO  启用或禁用印机构
     * @author wangchao
     * @date 2020/11/27 15:24
     * @param
     * @return
     */
    @Override
    @Transactional
    public boolean disableOrEnablePress(Integer id, Integer state) throws Exception {
        PressOrganization po=new PressOrganization();
        po.setId(id);
        po.setState(state);
        boolean bool=false;
        int i=pressOrganizationMapper.updateByPrimaryKeySelective(po);
        if(i>0){
            bool=true;
        }
        return bool;
    }
}
